----------------------------------------------------------------------------------------------------------------------------------
--[[



]]--
----------------------------------------------------------------------------------------------------------------------------------
--- 给遍历寻找所有电池用的
    local all_batteries = {}
    local function RegisterBattery(inst,flag)
        all_batteries[inst] = flag
    end
    local function RefreshAllBatteries()
        local new_table = {}
        for battery,flag in pairs(all_batteries) do
            if battery and battery:IsValid() then
                new_table[battery] = flag
            end
        end
        all_batteries = new_table
    end
    local function GetAllBatteries()
        RefreshAllBatteries()
        local ret = {}
        for battery,flag in pairs(all_batteries) do
            if battery and battery:IsValid() and flag then
                table.insert(ret, battery)
            end
        end
        return ret
    end
----------------------------------------------------------------------------------------------------------------------------------
---
    local function GetReplica(self)
        return self.inst.replica.loramia_com_power_battery or self.inst.replica._.loramia_com_power_battery
    end
    local function set_radius(self, radius)
        local replica_com = GetReplica(self)
        if replica_com then
            replica_com:SetRadius(radius)
        end
    end
    local function set_current(self, current)
        local replica_com = GetReplica(self)
        if replica_com then
            replica_com:SetCurrent(current)
        end
    end
    local function set_max(self, max)
        local replica_com = GetReplica(self)
        if replica_com then
            replica_com:SetMax(max)
        end
    end
    local function set_ignore_all_devices(self,value)
        local replica_com = GetReplica(self)
        if replica_com then
            replica_com:Set_Ignore_All_Devices(value)
        end
    end
----------------------------------------------------------------------------------------------------------------------------------
local loramia_com_power_battery = Class(function(self, inst)
    self.inst = inst
    inst:AddTag("loramia_com_power_battery")
    ---------------------------------------------------------------------------------------------------
    ---
        RegisterBattery(inst,true) -- 往总表注册
    ---------------------------------------------------------------------------------------------------
    ---
        self.DataTable = {}
        self.TempTable = {}
        self._onload_fns = {}
        self._onsave_fns = {}
    ---------------------------------------------------------------------------------------------------
    --- 发电半径
        self.radius = 10
        self:AddOnLoadFn(function()
            self.radius = self:Get("radius", 10)
        end)
        self:AddOnSaveFn(function()
            self:Set("radius", self.radius)
        end)
    ---------------------------------------------------------------------------------------------------
    --- current/max 电量
        self.current = 0
        self.max = 100
        self:AddOnLoadFn(function()
            self.current = self:Get("current", 0)
            self.max = self:Get("max", 100)
        end)
        self:AddOnSaveFn(function()
            self:Set("current", self.current)
            self:Set("max", self.max)
        end)
    ---------------------------------------------------------------------------------------------------
    --- 屏蔽所有链接设备
        self.ignore_all_devices = false
    ---------------------------------------------------------------------------------------------------

end,
nil,
{
    radius = set_radius,
    current = set_current,
    max = set_max,
    ignore_all_devices = set_ignore_all_devices,
})
------------------------------------------------------------------------------------------------------------------------------
--- 调取的公共API
    loramia_com_power_battery.GetAllBatteries = GetAllBatteries
------------------------------------------------------------------------------------------------------------------------------
--- radius
    function loramia_com_power_battery:SetRadius(radius)
        self.radius = radius
    end
    function loramia_com_power_battery:GetRadius()
        return self.radius
    end
------------------------------------------------------------------------------------------------------------------------------
--- 参数
    function loramia_com_power_battery:SetCurrent(current)
        local old_value = self.current
        self.current = math.clamp(current, 0, self.max)
        -- if old_value > 0 and self.current <= 0 then
        --     self.inst:PushEvent("empty")
        -- end
        self.inst:PushEvent("value_update",{
            old = old_value,
            new = self.current,
            on_empty = (old_value > 0 and self.current == 0 and true) or false,
            on_full =  (old_value < self.max and self.current == self.max and true) or false,
        })
    end
    function loramia_com_power_battery:GetCurrent()
        return self.current
    end
    function loramia_com_power_battery:SetMax(max)
        self.max = max
        self:SetCurrent(self.current)
    end
    function loramia_com_power_battery:GetMax()
        return self.max
    end
    function loramia_com_power_battery:SetPercent(value)
        local percent = math.clamp(value, 0, 1)
        self:SetCurrent(self.max * percent)
    end
    function loramia_com_power_battery:GetPercent()
        return self.current / self.max
    end
    function loramia_com_power_battery:DoDelta(delta)
        self:SetCurrent(self.current + delta)
    end
    function loramia_com_power_battery:Has(value)
        return self.current >= value
    end
    function loramia_com_power_battery:IsEmpty()
        return self.current <= 0
    end
    function loramia_com_power_battery:IsFull()
        return self.current >= self.max
    end
------------------------------------------------------------------------------------------------------------------------------
--- 屏蔽所有链接设备
    function loramia_com_power_battery:Set_Ignore_All_Devices(flag)
        self.ignore_all_devices = flag
        RegisterBattery(self.inst,flag)
    end
------------------------------------------------------------------------------------------------------------------------------







------------------------------------------------------------------------------------------------------------------------------
----- onload/onsave 函数
    function loramia_com_power_battery:AddOnLoadFn(fn)
        if type(fn) == "function" then
            table.insert(self._onload_fns, fn)
        end
    end
    function loramia_com_power_battery:ActiveOnLoadFns()
        for k, temp_fn in pairs(self._onload_fns) do
            temp_fn(self)
        end
    end
    function loramia_com_power_battery:AddOnSaveFn(fn)
        if type(fn) == "function" then
            table.insert(self._onsave_fns, fn)
        end
    end
    function loramia_com_power_battery:ActiveOnSaveFns()
        for k, temp_fn in pairs(self._onsave_fns) do
            temp_fn(self)
        end
    end
------------------------------------------------------------------------------------------------------------------------------
----- 数据读取/储存
    function loramia_com_power_battery:Get(index,default)
        if index then
            return self.DataTable[index] or default
        end
        return nil
    end
    function loramia_com_power_battery:Set(index,theData)
        if index then
            self.DataTable[index] = theData
        end
    end

    function loramia_com_power_battery:Add(index,num,min,max)
        if index then
            if not min and not max then
                self.DataTable[index] = (self.DataTable[index] or 0) + ( num or 0 )
                return self.DataTable[index]
            elseif type(min) == "number" and type(max) == "number" then
                local ret = math.clamp( (self.DataTable[index] or 0) + ( num or 0 ) , min , max )
                self.DataTable[index] = ret
                return ret
            end
        end
        return 0
    end
------------------------------------------------------------------------------------------------------------------------------
--- Save/Load
    function loramia_com_power_battery:OnSave()
        self:ActiveOnSaveFns()
        local data =
        {
            DataTable = self.DataTable
        }
        return next(data) ~= nil and data or nil
    end

    function loramia_com_power_battery:OnLoad(data)
        if data.DataTable then
            self.DataTable = data.DataTable
        end
        self:ActiveOnLoadFns()
    end
------------------------------------------------------------------------------------------------------------------------------
return loramia_com_power_battery







